package com.smart.mip.system.common;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;

/**
 * @author sx
 * @create 2013年12月30日
 */

@Controller
public class BaseController {
    
    /**
     * 转换查询参数为字符串，用于 = 或者 like 查询
     */
    public static final String SEARCH_STRING_PREFIX = "s_";

    public static final int SEARCH_STRING_PREFIX_LEN = 2;
    
    /**
     * 转换查询参数为字符串数组，用于 in 查询
     */
    public static final String SEARCH_ARRAY_PREFIX = "a_";

    public static final int SEARCH_ARRAY_PREFIX_LEN = 2;
    
    /**
     * 转换查询参数为数字，用于 Long 类型的 = 或者 like 查询
     */
    public static final String SEARCH_NUMBER_PREFIX = "n_";
    
    public static final int SEARCH_NUMBER_PREFIX_LEN = 2;
    
    /**
     * 转换查询参数为数字数组，用于 Long 类型的 in 查询
     */
    public static final String SEARCH_NUMBER_ARRAY_PREFIX = "na_";
    
    public static final int SEARCH_NUMBER_ARRAY_PREFIX_LEN = 3;
    

    protected Log logger = LogFactory.getLog(BaseController.class);
    /**
     * 当前log4j日志是否打开Debug模式
     */
    protected boolean logDebug = logger.isDebugEnabled();

    protected static final String PAGE_DESC = "pageDesc";

    protected static final String OBJECT = "object";

    protected static final String OBJLIST = "objList";

    public static void setbackSearchColumn(Map<String, Object> filterMap, HttpServletRequest request) {
        if (filterMap == null || filterMap.size() < 1) {
            return;
        }
        for (Map.Entry<String, Object> ent : filterMap.entrySet()) {
            Object objValue = ent.getValue();
            String skey;
            if(objValue instanceof String ){
                //和default一样，为了效率优先判断
                skey = SEARCH_STRING_PREFIX + ent.getKey();
            }else if(objValue instanceof String[]){
                skey = SEARCH_ARRAY_PREFIX + ent.getKey();
            }else if(objValue instanceof Long){
                skey = SEARCH_NUMBER_PREFIX + ent.getKey();
            }else if(objValue instanceof Long[]){
                skey = SEARCH_NUMBER_ARRAY_PREFIX + ent.getKey();
            }else{
                skey = SEARCH_STRING_PREFIX + ent.getKey();
            }
            request.setAttribute(skey, objValue);
        }
    }
    
}
